#!/usr/bin/env python

import sys, os, os.path
import fontforge
import string

__ALPHA = list(string.ascii_lowercase)

def getalphaglyphs(font):
    return [x for x in font.glyphs() if x.glyphname in __ALPHA]
    

def export_bmps(font, alpha_glyphs, dest, format='bmp', resol=19, depth=1, target=__ALPHA):
    for leter in alpha_glyphs:
        leter.export(os.path.join(dest, '%s.%s' % (leter.glyphname, format)), resol, depth)

def main():
    for arg in sys.argv[1:]:
        try:
            font = fontforge.open(arg)
            alpha_glyphs = getalphaglyphs(font)
            if alpha_glyphs:
                os.mkdir(font.fontname)
            else:
                print font.fontname, 'has no chararcters in', __ALPHA
        except OSError as ose:
            print arg, 'trying to create folder to already existent fontname', font.fontname
        except EnvironmentError as ee:
            print arg, "couldn't be open"
        else:
            export_bmps(font, alpha_glyphs, font.fontname)
            font.close()
    return 0

if __name__ == '__main__':
    sys.exit(main())
